#!/usr/bin/python
# -*- coding: UTF-8 -*-
import threading
import tkinter
from WebSite.BossWebSite import BossWebSite

class Test(object):

    def __init__(self):
        self.bossWebSite = None

    def parseCallback(self, index, size):
        if index+1 == size:
            b_load["state"] = "normal"
            b_load_detail["state"] = "normal"
            l_tips.config(text = "finished ")
        else:
            l_tips.config(text="parsing %d/%d" % (index + 1, size))

    def saveCallback(self, index, size):
        if index+1 == size:
            b_save["state"] = "normal"
            l_tips.config(text = "finished ")
        else:
            l_tips.config(text="saving %d/%d" % (index + 1, size))

    def threadLoadPage(self, page):
        self.bossWebSite.loadPage(page)
        self.bossWebSite.parseData()

    def threadLoadDetailPage(self):
        self.bossWebSite.loadAndParseDetailPage()

    def loadPage(self,page):
        if self.bossWebSite is None:
            self.bossWebSite = BossWebSite()

        self.bossWebSite.setParseCallback(callback=self.parseCallback)

        b_load["state"] = "disabled"
        b_load_detail["state"] = "disabled"
        th = threading.Thread(target=self.threadLoadPage, args=(page,))
        th.setDaemon(True)  # 守护线程
        th.start()
        pass

    def loadDetailPage(self):
        if self.bossWebSite is None:
            self.bossWebSite = BossWebSite()

        self.bossWebSite.setParseCallback(callback=self.parseCallback)

        b_load["state"] = "disabled"
        b_load_detail["state"] = "disabled"
        th = threading.Thread(target=self.threadLoadDetailPage)
        th.setDaemon(True)  # 守护线程
        th.start()
        pass

    def saveData(self):
        b_save["state"] = "disabled"
        b_load_detail["state"] = "disabled"
        self.bossWebSite.setSaveCallback(callback=self.saveCallback)
        th = threading.Thread(target=self.bossWebSite.saveExcel, args=('D:/python_workspace/WebCrawler/list.xls',))
        th.setDaemon(True)  # 守护线程
        th.start()

    def saveDetailData(self):
        b_save["state"] = "disabled"
        b_load_detail["state"] = "disabled"
        self.bossWebSite.setSaveCallback(callback=self.saveCallback)
        th = threading.Thread(target=self.bossWebSite.saveDetailExcel, args=('D:/python_workspace/WebCrawler/detail.xls',))
        th.setDaemon(True)  # 守护线程
        th.start()

test = Test()
root = tkinter.Tk()
root.title('数据挖掘神器')
#第一行
l_page = tkinter.Label(root, text='Page：')
l_page.grid(row=0, sticky=tkinter.W)
page = tkinter.StringVar()
e_page = tkinter.Entry(root, textvariable=page)
e_page.grid(row=0, column=1, sticky=tkinter.E)
page.set('1')

#第二行
l_tips = tkinter.Label(root, text='tips')
l_tips.grid(row=1, column=0, stick=tkinter.W, pady=10)

b_load = tkinter.Button(root, text='Load List', command=lambda:test.loadPage(page.get()))
b_load.grid(row=1, column=1, sticky=tkinter.E)

b_save = tkinter.Button(root, text='Save', command=test.saveData)
b_save.grid(row=1, column=2, sticky=tkinter.E)

#第三行
b_load_detail = tkinter.Button(root, text='Load Detail', command=test.loadDetailPage)
b_load_detail.grid(row=2, column=0, sticky=tkinter.E)

b_save_detail = tkinter.Button(root, text='Save', command=test.saveDetailData)
b_save_detail.grid(row=2, column=2, sticky=tkinter.E)

root.resizable(0,0) #阻止Python GUI的大小调整
# 进入消息循环
root.mainloop()


